<?php 
/**
 * QiaQiaCMS PDO 类库
 * ============================================================================
 * * 版权所有 维赛网络工作室，并保留所有权利。
 * 网站地址: http://www.weisai.net；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: hungry $
 * $Id: pdoDB.php  $
 * $pdo=pdoDB::getInstance('sqlite:'.DB_PATH.'7'.'/db.db');
 */
if (!defined('QIAQIA'))
{
	die('Illegal Access');
}
Class pdoDB
{
	//pdo对象
	    private $_pdo = null;
	    //用于存放实例化的对象
	    static private $_instance = null;
	
	    //公共静态方法获取实例化的对象
	    static public function getInstance($db_dns='',$db_user='',$db_pass='',$db_charset='UTF8') {
		        if (!(self::$_instance instanceof self)) {
			           self::$_instance = new self($db_dns,$db_user,$db_pass,$db_charset);
			        }
			       return self::$_instance;
			    }
			
			    //私有克隆
		private function __clone() {}
			
			    //私有构造
		private function __construct($db_dns='',$db_user='',$db_pass='',$db_charset='UTF8') {
		try {
			 $this->_pdo = new PDO($db_dns, $db_user, $db_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES '.$db_charset));
			 $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//设置异常可捕获
					        } catch (PDOException $e) {
						            exit($e->getMessage());
						        }
						    }
		//查询一条记录				    
		public function getOne($_sql)
		{
			$_sql = trim($_sql . ' LIMIT 1');
			$_stmt=$this->execute($_sql);
			$_objs = $_stmt->fetch(PDO::FETCH_ASSOC);
			return $_objs[0];
        }
        //查询多条记录
        public function getAll($_sql)
        {
        	$_stmt=$this->execute($_sql);
        	$_objs = $_stmt->fetchAll(PDO::FETCH_ASSOC);
        	
        	return $_objs;
        }
        //查询多列
        public function getCol($_sql)
        {
        	$_stmt=$this->execute($_sql);
        	$_objs = $_stmt->fetchAll(PDO::FETCH_ASSOC);
        	$res=array();
        	if(is_array($_objs))
        	{
        		foreach($_objs as $k=>$v)
        		{
        			$res[]=current($v);
        		}
        	}
        	
        	return $res;
        }
        //查询一行
         public function getRow($_sql)
         {
         	$_sql = trim($_sql . ' LIMIT 1');
         	$_stmt=$this->execute($_sql);
         	$_objs = $_stmt->fetch(PDO::FETCH_ASSOC);
         	return $_objs;
         }	
       
				 //新增
	   public function insert(Array $_addData,$_tables) {
							        $_addFields = array();
							        $_addValues = array();
							        foreach ($_addData as $_key=>$_value) {
								            $_addFields[] = $_key;
							           $_addValues[] = $_value;
								        }
								        $_addFields = implode(',', $_addFields);
								        $_addValues = implode("','", $_addValues);
								        $_sql = "INSERT INTO $_tables ($_addFields) VALUES ('$_addValues')";
								        return $this->execute($_sql)->rowCount();
								    }
								
								    //修改
	 public function update(Array $_updateData,$_tables, Array $_param ) {
			 $_where = $_setData = '';
			foreach ($_param as $_key=>$_value) {
					$_where .= $_value.' AND ';
					}
					$_where = 'WHERE '.substr($_where, 0, -4);
					foreach ($_updateData as $_key=>$_value) {
					if (is_array($_value)) {
					$_setData .= "$_key=$_value[0],";
					} else {
					 $_setData .= "$_key='$_value',";
					}
					}
					$_setData = substr($_setData, 0, -1);
					$_sql = "UPDATE $_tables SET $_setData $_where";
					return $this->execute($_sql)->rowCount();
													    }
													
	 //验证一条数据
	protected function isOne($_tables, Array $_param) {
														        $_where = '';
														        foreach ($_param as $_key=>$_value) {
															            $_where .=$_value.' AND ';
															        }
															        $_where = 'WHERE '.substr($_where, 0, -4);
															        $_sql = "SELECT id FROM $_tables[0] $_where LIMIT 1";
															        return $this->execute($_sql)->rowCount();
															    }
															
	//删除
	public function delete($_tables, Array $_param) {
				$_where = '';
				 foreach ($_param as $_key=>$_value) {
					    $_where .= $_value.' AND ';
				}
				 $_where = 'WHERE '.substr($_where, 0, -4);
				 $_sql = "DELETE FROM $_tables $_where LIMIT 1";
				return $this->execute($_sql)->rowCount();
				}
																	
	
//总记录
   public function count($_tables, Array $_param = array()) {
       $_where = '';
        if (isset($_param)) {
            foreach ($_param as $_key=>$_value) {
                $_where .= $_value.' AND ';
            }
            $_where = 'WHERE '.substr($_where, 0, -4);
        }
     
        $_sql = "SELECT COUNT(*) as count FROM $_tables $_where";   echo $_sql; 
        $_stmt = $this->execute($_sql);
       return $_stmt->fetchObject()->count;
    }

    //得到上次插入ID
    public function insert_id() {
        
        return $this->_pdo->lastInsertId();
    }

    //执行SQL
   private function execute($_sql) {
       try {
           $_stmt = $this->_pdo->prepare($_sql);
            $_stmt->execute();
       } catch (PDOException  $e) {
            exit('SQL语句：'.$_sql.'<br />错误信息：'.$e->getMessage());
       }
        return $_stmt;
   }
}